﻿Public Class CodeMaker

    Private connectedClient As ConnectedClient
    Private mastermind As Mastermind
    Private hasCode As Boolean
    Private activeCodeRow As CodePegRow
    Private activeKeyRow As KeyPegRow

    Private Sub CodeMaker_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        connectedClient = ConnectForm.MyConnectedClient
        mastermind = ConnectForm.MyMastermind
        hasCode = False
        activeCodeRow = CodePegRow1
        activeKeyRow = KeyPegRow1

        InitializeCodeMakerForm()
    End Sub

    Private Sub InitializeCodeMakerForm()
        Label1.Text = "Waiting for an opponent"
        Label2.Text = "Score: " + mastermind.GetScore().ToString()
        Button1.Enabled = False
        Button2.Enabled = False
        InitializeCodePegList()
        InitializeKeyPegList()
        InitializeCodePegBoard()
        InitializeKeyPegBoard()
        InitializeCode()
    End Sub

    Private Sub InitializeCodePegList()
        CodePegList1.InitializeCodePegs()
    End Sub

    Private Sub InitializeKeyPegList()
        KeyPegList1.InitializeKeyPegs()
    End Sub

    Private Sub InitializeCodePegBoard()
        CodePegRow1.InitializeCodePegRow()
        CodePegRow2.InitializeCodePegRow()
        CodePegRow3.InitializeCodePegRow()
        CodePegRow4.InitializeCodePegRow()
        CodePegRow5.InitializeCodePegRow()
        CodePegRow6.InitializeCodePegRow()
        CodePegRow7.InitializeCodePegRow()
        CodePegRow8.InitializeCodePegRow()
        CodePegRow9.InitializeCodePegRow()
        CodePegRow10.InitializeCodePegRow()
    End Sub

    Private Sub InitializeKeyPegBoard()
        KeyPegRow1.InitializeKeyPegRow()
        KeyPegRow2.InitializeKeyPegRow()
        KeyPegRow3.InitializeKeyPegRow()
        KeyPegRow4.InitializeKeyPegRow()
        KeyPegRow5.InitializeKeyPegRow()
        KeyPegRow6.InitializeKeyPegRow()
        KeyPegRow7.InitializeKeyPegRow()
        KeyPegRow8.InitializeKeyPegRow()
        KeyPegRow9.InitializeKeyPegRow()
        KeyPegRow10.InitializeKeyPegRow()
    End Sub

    Private Sub InitializeCode()
        Code1.InitializeCode()
    End Sub

    Public Sub EnableUI()
        If Not hasCode Then
            CodePegList1.Enable()
            Code1.Enable()
        Else
            Dim row As Integer = mastermind.GetRow
            Select Case row
                Case 0
                    KeyPegRow1.Enable()
                    activeCodeRow = CodePegRow1
                    activeKeyRow = KeyPegRow1
                Case 1
                    KeyPegRow2.Enable()
                    activeCodeRow = CodePegRow2
                    activeKeyRow = KeyPegRow2
                Case 2
                    KeyPegRow3.Enable()
                    activeCodeRow = CodePegRow3
                    activeKeyRow = KeyPegRow3
                Case 3
                    KeyPegRow4.Enable()
                    activeCodeRow = CodePegRow4
                    activeKeyRow = KeyPegRow4
                Case 4
                    KeyPegRow5.Enable()
                    activeCodeRow = CodePegRow5
                    activeKeyRow = KeyPegRow5
                Case 5
                    KeyPegRow6.Enable()
                    activeCodeRow = CodePegRow6
                    activeKeyRow = KeyPegRow6
                Case 6
                    KeyPegRow7.Enable()
                    activeCodeRow = CodePegRow7
                    activeKeyRow = KeyPegRow7
                Case 7
                    KeyPegRow8.Enable()
                    activeCodeRow = CodePegRow8
                    activeKeyRow = KeyPegRow8
                Case 8
                    KeyPegRow9.Enable()
                    activeCodeRow = CodePegRow9
                    activeKeyRow = KeyPegRow9
                Case 9
                    KeyPegRow10.Enable()
                    activeCodeRow = CodePegRow10
                    activeKeyRow = KeyPegRow10
            End Select

            KeyPegList1.Enable()
            Button2.Enabled = True
        End If

        Label1.Text = "It's your turn"    
        Button1.Enabled = True
    End Sub

    Public Sub DisableUI()
        KeyPegList1.Disable()

        KeyPegRow1.Disable()
        KeyPegRow2.Disable()
        KeyPegRow3.Disable()
        KeyPegRow4.Disable()
        KeyPegRow5.Disable()
        KeyPegRow6.Disable()
        KeyPegRow7.Disable()
        KeyPegRow8.Disable()
        KeyPegRow9.Disable()
        KeyPegRow10.Disable()

        CodePegList1.Disable()
        Code1.Disable()

        Button1.Enabled = False
        Button2.Enabled = False
    End Sub

    Public Sub Decode(ByVal data As String)
        EnableUI()

        ' Decode the message sent and keep the information to mastermind's CodePeg array
        mastermind.Decode(data, mastermind.GetCodePegBoard)

        ' Get the active row
        Dim row As Integer = mastermind.GetRow
        ' Get the CodePeg array's information
        Dim array(,) As String = mastermind.GetCodePegBoard

        ' Iterate through CodePeg's active row
        ' Set correct colors to UI (by changing active code row picture boxes)
        For i As Integer = 0 To array.GetUpperBound(1)
            activeCodeRow.SetColor(i, array(row, i))
        Next

        mastermind.IncreaseScore(1)
        Label2.Text = "Score: " + mastermind.GetScore().ToString()

        ' Check if CodeBreaker broke the code
        Dim win As Boolean = mastermind.CheckWinCondition()

        ' Code has been broken
        If win Then
            MsgBox("Your opponent broke the code!")
            DisableUI()

            If mastermind.GetActiveGame = mastermind.GetMaxGames Then
                connectedClient.SendData("Score." + mastermind.GetScore().ToString)
            Else
                MsgBox("Get ready for round 2")
                connectedClient.SendData("Next round")
                mastermind.IncreaseActiveGame()
                mastermind.ResetGame()
                Me.Hide()
                CodeBreaker.Show()
            End If
        ElseIf mastermind.GetRow = (mastermind.GetMaxTries - 1) Then
            MsgBox("Your opponent couldn't break the code")
            DisableUI()

            mastermind.IncreaseScore(1)
            Label2.Text = "Score: " + mastermind.GetScore().ToString()

            If mastermind.GetActiveGame = mastermind.GetMaxGames Then
                connectedClient.SendData("Score." + mastermind.GetScore().ToString)
            Else
                MsgBox("Get ready for round 2")
                connectedClient.SendData("Next round")
                mastermind.IncreaseActiveGame()
                mastermind.ResetGame()
                Me.Hide()
                CodeBreaker.Show()
            End If
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        activeKeyRow.Reset()
        mastermind.Reset()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim validation As Boolean = False

        ' If hasCode = True, then we already have a color code set
        ' So we validate player hints
        ' Otherwise we check if color code is correct
        If hasCode Then
            mastermind.SetKeyRow(mastermind.GetRow, activeKeyRow.GetKeyRow)

            validation = mastermind.ValidateHints()

            If Not validation Then
                Label1.Text = "Please check your hints again and place them correctly"
            Else
                Dim keyRow() As String = activeKeyRow.GetKeyRow
                Dim message As String = mastermind.Encode(keyRow)
                connectedClient.SendData(message)
                Label1.Text = "Waiting for opponents turn"
                DisableUI()
                mastermind.IncreaseRow()
            End If
        Else
            mastermind.SetCode(Code1.GetCode)

            ' Check if user putted colors in each slot of the code
            validation = mastermind.ValidateCode()

            If Not validation Then
                Label1.Text = "All slots of code must be filled before proceeding"
            Else
                hasCode = True
                connectedClient.SendData("Start")
                Label1.Text = "Waiting for opponents turn"
                DisableUI()
            End If
        End If
    End Sub

    Private Sub CodeMaker_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        connectedClient.SendData("Disconnected")
        connectedClient.ShutDownClient()

        ConnectForm.Close()
    End Sub

End Class